package org.dromara.wmscloud.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.wmscloud.domain.bo.BaseUnitBo;
import org.dromara.wmscloud.domain.vo.BaseUnitVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.wmscloud.domain.vo.BaseMatClassVo;
import org.dromara.wmscloud.domain.bo.BaseMatClassBo;
import org.dromara.wmscloud.service.IBaseMatClassService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 物料分类
 *
 * @author hou
 * @date 2025-04-04
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/wmsCloud/matClass")
public class BaseMatClassController extends BaseController {

    private final IBaseMatClassService baseMatClassService;

    /**
     * 查询物料分类列表
     */
    @SaCheckPermission("base:class:list")
    @GetMapping("/listAll")
    public R <List<BaseMatClassVo>> listAllMatClass() {
        BaseMatClassBo bo = new BaseMatClassBo();
        List<BaseMatClassVo> list = baseMatClassService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 查询物料分类列表
     */
    @SaCheckPermission("base:class:list")
    @GetMapping("/list")
    public TableDataInfo<BaseMatClassVo> list(BaseMatClassBo bo, PageQuery pageQuery) {
        return baseMatClassService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出物料分类列表
     */
    @SaCheckPermission("base:class:export")
    @Log(title = "物料分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BaseMatClassBo bo, HttpServletResponse response) {
        List<BaseMatClassVo> list = baseMatClassService.queryList(bo);
        ExcelUtil.exportExcel(list, "物料分类", BaseMatClassVo.class, response);
    }

    /**
     * 获取物料分类详细信息
     *
     * @param classId 主键
     */
    @SaCheckPermission("base:class:query")
    @GetMapping("/{classId}")
    public R<BaseMatClassVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long classId) {
        return R.ok(baseMatClassService.queryById(classId));
    }

    /**
     * 新增物料分类
     */
    @SaCheckPermission("base:class:add")
    @Log(title = "物料分类", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BaseMatClassBo bo) {
        return toAjax(baseMatClassService.insertByBo(bo));
    }

    /**
     * 修改物料分类
     */
    @SaCheckPermission("base:class:edit")
    @Log(title = "物料分类", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BaseMatClassBo bo) {
        return toAjax(baseMatClassService.updateByBo(bo));
    }

    /**
     * 删除物料分类
     *
     * @param classIds 主键串
     */
    @SaCheckPermission("base:class:remove")
    @Log(title = "物料分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{classIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] classIds) {
        return toAjax(baseMatClassService.deleteWithValidByIds(List.of(classIds), true));
    }
}
